import config from "@/config/index.js";
import store from "@/store/store.js"
import utils from "@/common/utils/utils.js";
const baseUrl = config.baseUrl;

function checkPhone() {
	let url = baseUrl + '/wechat/mobileStatus'
	uni.request({
		url: url,
		method: "POST",
		header: {
			// 数据被编码为名称/值对
			"content-type": "application/json",
			"user-token": store.state.vuex_token,
			"channel":'2'
		}, 
	}).then((res) => {
		//获取到 openid 和 session_k后，自己的逻辑
		if (res[1].data.mobile_status) {
			store.commit("SET_USERINFO", res[1].data.user);
            uni.setStorageSync('userinfo', res[1]
            	.data.user);
			uni.reLaunch({
			    url: '/pages/tabBar/home'
			})
			// console.log(store.state.vuex_token)
		} else {
			uni.navigateTo({ //跳转至绑定手机号
				url: '../../pagesA/bindingPhone/index'
			});
		}

	});
}

function wx_authLogin() {
	////弹窗授权
    return
	uni.showModal({
		title: '授权登录',
		content: '是否授权登录微信小程序？',
		success: (res) => {
			// 开始微信授权登录
            const url = baseUrl
            if(res.confirm == true){
            uni.login({
            	provider: 'weixin',
            	success: function(loginRes) {
            		// 获取用户信息
            		uni.getUserProfile({
            			provider: 'weixin',
            			fail() {
            				uni.showToast({
            				    title:'授权失败',
                                icon:'none'
            				})
            			},
            			success: function(infoRes) {
            				var wxinfo = infoRes.userInfo
                            console.log( wxinfo ,'获取用户信息')
                            uni.request({
                            	url: url + '/wechat/appMiniLogin', //app 登录接口地址
                            	method: 'POST',
                            	data: {
                            		unionid: wxinfo.unionId,
                            		openid: wxinfo.openId,
                            		nickName: wxinfo.nickName,
                            		headurl: wxinfo.avatarUrl,
                            	},
                                header: {
                                	// 数据被编码为名称/值对
                                	"content-type": "application/json",
                                },
                            	success: res => {
                            		// console.log(res , '微信登录返回信息')
                                    if(res.data.code == 200){
                                        uni.showToast({
                                            title:'登录成功',
                                            icon:"none"
                                        })
                                        console.log(res.data.token)
                                        store.commit("SET_TOKEN", res.data.token);
                                        uni.setStorageSync('token', res.data.token);
                                        let nowTime = Date.parse(
                                        	new Date()) / 1000;
                                        let expire = nowTime + 3 * 3600 * 24;
                                        uni.setStorageSync('timer', expire);
                                        checkPhone()
                                    }
                            	},
                            	fail(err) {
                            		console.log(err , '微信登录返回错误信息')
                            	}
                            });
            			}
            		});
            	},
            	fail(err) {
            		//console.log("进入登录接口报错", err)
            	}
            });
            }
		}
	})
}


export default wx_authLogin;
